package com.project.movie.controller.recommand;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.movie.common.utils.ItemCF;
import com.project.movie.config.utils.ShiroUtils;
import com.project.movie.domain.*;
import com.project.movie.service.MovieFavorService;
import com.project.movie.service.MovieItemService;
import com.project.movie.service.MovieLikeService;
import com.project.movie.service.MovieOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("recommand")
public class RecommandController {

    @Autowired
    private MovieFavorService movieFavorService;
    @Autowired
    private MovieLikeService movieLikeService;
    @Autowired
    private MovieOrderService movieOrderService;
    @Autowired
    private MovieItemService movieItemService;

    @GetMapping("getRecommandMovie")
    public Result getRecommandMovie() {
        User user = ShiroUtils.getUserInfo();
        //先获取用户喜欢的电影的类型
        List<RelateDTO> list = new ArrayList<>();
        Map<String,Double> map = new HashMap<>();
        //获取收藏
        QueryWrapper<MovieFavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MovieFavor::getUserId,user.getId());
        List<MovieFavor> favorList = movieFavorService.list(queryWrapper);
        for (MovieFavor favor : favorList) {
            MovieItem movieItem = movieItemService.getById(favor.getItemId());
            if (movieItem != null) {
                String[] strings = movieItem.getType().split(",");
                for (String type : strings) {
                    if (map.containsKey(type)) {
                        Double aDouble = map.get(type);
                        map.put(type,aDouble + 1);
                    } else {
                        map.put(type,1d);
                    }
                }
            }
        }
        //获取喜欢
        QueryWrapper<MovieLike> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(MovieLike::getUserId,user.getId());
        List<MovieLike> likeList = movieLikeService.list(queryWrapper1);
        for (MovieLike like : likeList) {
            MovieItem movieItem = movieItemService.getById(like.getItemId());
            if (movieItem != null) {
                String[] strings = movieItem.getType().split(",");
                for (String type : strings) {
                    if (map.containsKey(type)) {
                        Double aDouble = map.get(type);
                        map.put(type,aDouble + 1);
                    } else {
                        map.put(type,1d);
                    }
                }
            }
        }
        //获取已购订单
        QueryWrapper<MovieOrder> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().eq(MovieOrder::getUserId,user.getId());
        List<MovieOrder> orderList = movieOrderService.list(queryWrapper2);
        for (MovieOrder order : orderList) {
            MovieItem movieItem = movieItemService.getById(order.getItemId());
            if (movieItem != null) {
                String[] strings = movieItem.getType().split(",");
                for (String type : strings) {
                    if (map.containsKey(type)) {
                        Double aDouble = map.get(type);
                        map.put(type,aDouble + 1);
                    } else {
                        map.put(type,1d);
                    }
                }
            }
        }
        //完事之后循环map
        for (String key : map.keySet()) {
            RelateDTO relateDTO = new RelateDTO();
            relateDTO.setItemId(key);
            relateDTO.setIndex(map.get(key));
            list.add(relateDTO);
        }
        //获取用户最常看的一个电影
        String type = "";
        Double height = 0d;
        for (RelateDTO relateDTO : list) {
            if (relateDTO.getIndex() > height) {
                height = relateDTO.getIndex();
                type = relateDTO.getItemId();
            }
        }
        if (list.size() == 0) {
            List<MovieItem> movieItemList = movieItemService.getRecommandItem(new ArrayList<>());
            return Result.success(movieItemList);
        } else {
            // 获取到推荐的电影id
            List<String> recommendations = ItemCF.recommend(type, list);
            //查询爱看的电影
            List<MovieItem> movieItemList = movieItemService.getRecommandItem(recommendations);
            return Result.success(movieItemList);
        }
    }
}
